package com.daervin.svc.parser.hot;

import com.daervin.svc.dal.model.HotItem;
import com.daervin.svc.parser.RootParser;
import com.sun.javafx.binding.StringFormatter;
import org.apache.commons.collections.CollectionUtils;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.daervin.svc.common.constants.HotSiteEnum.TODAY_HUB;

/**
 * @packageName: com.daervin.svc.parser.hot
 * @CLassName: TodayHubParser
 * @Description:
 * @Author: wanxiong.deng
 * @Date: 2021/4/26 16:17
 */
public class TodayHubParser extends RootParser {
    public static String ss = "//div[@class=\"%s\"]";
    public  TodayHubParser(String... url){
        super(url);
    }
    @Override
    protected void listProcess(Page page) {
        Html html = page.getHtml();
//        List<Selectable> itemList = html.xpath("//div[@class=\"cc-cd-cb-l nano-content\"]").nodes();
        List<Selectable> itemList = html.xpath(String.format(ss, "cc-cd-cb-l nano-content")).nodes();
        //List<Selectable> sList = html.xpath("//div[@class=\"cc-cd-lb\"]").nodes();
        List<Selectable> sList = html.xpath(String.format(ss, "cc-cd-lb")).nodes();
        //List<String> companyList = sList.stream().map(x->x.xpath("//div[@class=\"cc-cd-lb\"]/span/text()").get()).collect(Collectors.toList());
        List<String> companyList = sList.stream().map(x->x.xpath(String.format(ss,"cc-cd-lb")+"/span/text()").get()).collect(Collectors.toList());
        List<HotItem> hotItems = new ArrayList<>();
        if(CollectionUtils.isEmpty(itemList)){
            return;
        }
        int i= 0;
        for(Selectable item: itemList){
            List<Selectable> aList = item.xpath("//a").nodes();
            for(Selectable a: aList){
                String links = a.xpath("//a").links().get();
                String title = a.xpath("//div[@class=\"cc-cd-cb-ll\"]/span[@class=\"t\"]/text()").get();
                String clicks = a.xpath("//div[@class=\"cc-cd-cb-ll\"]/span[@class=\"e\"]/text()").get();
                hotItems.add(new HotItem(TODAY_HUB.ID, title, links, clicks, companyList.get(i)));
            }
            i++;
        }
        if(CollectionUtils.isEmpty(hotItems)){
            return;
        }
        page.putField("today", hotItems);
    }

    @Override
    public Site getSite() {
        return super.getSite();
    }
}
